package ws.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import ws.log.*;


/**
 * This class implements the db open and close operation.
 * Other db operate classes such as ExecSQL class and ReadTable class must be inherit it.
 * 
 * */
public class DBOpenClose {
	private boolean directconn;
	private static final String Lookup = "java:comp/env/jdbc/cuczz";
	
	public DBOpenClose(){
		directconn = false;
	}
	
	/**
	 * @param directconn the directconn to set
	 */
	public void setDirectconn(boolean directconn) {
		this.directconn = directconn;
	}
	
	
	/**
	 * Init Connection.
	 * @param: void
	 * @return:
	 * 		Connection - the new init connection.
	 * @exception:
	 * 		exception has been written to log file.
	 * */
	public Connection getConn(){
		
		if(directconn){
			return getConnDirect();
		}else{
			return getConnFromPool();
		}
		
	
	}
	
	/**
	 * Create connection use db driver directly connect.
	 * This function will be used in debug .
	 * @exception:
	 * 		exception has been written to log file.
	 * */
	private Connection getConnDirect(){
		Connection conn = null;
		try{
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
			String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CUCZZ"; 
			String user="cuczz"; 
			String password="cuczz"; 
			conn= DriverManager.getConnection(url,user,password); 
		}catch(ClassNotFoundException classNotFoundEx){
			Log.writeDBMiddleLayerLog(this.getClass()+"直接进行连接错误", classNotFoundEx);
		}catch(InstantiationException instantEx){
			Log.writeDBMiddleLayerLog(this.getClass()+"直接进行连接错误", instantEx);
		}catch(IllegalAccessException illegalAccessEx){
			Log.writeDBMiddleLayerLog(this.getClass()+"直接进行连接错误",illegalAccessEx );
		}catch(SQLException e){
			Log.writeDBMiddleLayerLog(this.getClass()+"直接进行连接错误", e);
		}
		return conn;
	}
	
	/**
	 * Create connection use db connection pool.
	 * @exception:
	 * 		exception has been written to log file.
	 * */
	private Connection getConnFromPool(){
		Connection conn = null;
		try{
			Context context = new InitialContext();
			DataSource ds = (DataSource)context.lookup(Lookup);
			conn = ds.getConnection();
		}catch(NamingException namingEx){
			Log.writeDBMiddleLayerLog(this.getClass()+"从数据池得到连接错误", namingEx);
		}catch(SQLException ex){
			Log.writeDBMiddleLayerLog(this.getClass()+"从数据池得到连接错误", ex);
		}
		return conn;
	}
	
	/**
	 * Close the db connection.
	 * @param:
	 * 		conn -  the Connection object.
	 * 		stmt - 	the Statement object. If you didn`t use the set null.
	 * 		rs 	 -	the ResultSet object. If you didn`t use the set null.
	 * @return: void.
	 * @exception:
	 * 		exception has been writeen to log file. 
	 * */
	public void close(Connection conn,Statement stmt,ResultSet rs){
		try{
			if(rs != null)
				rs.close();
			if(stmt != null)
				stmt.close();
			if(conn != null)
				conn.close();
		}catch(SQLException ex){
			Log.writeDBMiddleLayerLog(this.getClass()+"关闭数据库连接", ex);
		}	
	}
}
